from db.db_base import MainBaseModel
from sqlalchemy import Column, String, Boolean, DateTime
from passlib.context import CryptContext

pwd_context = CryptContext(schemes=['bcrypt'], deprecated='auto')


class VadminAuthUser(MainBaseModel):
    __tablename__ = "vadmin_auth_user"
    __table_args__ = ({'comment': '主管理系统用户表'})

    avatar = Column(String(500), comment='头像')
    telephone = Column(String(11), nullable=False, index=True, comment="手机号", unique=True)
    name = Column(String(50), index=True, nullable=False, comment="姓名")
    nickname = Column(String(50), nullable=True, comment="昵称")
    password = Column(String(255), nullable=True, comment="密码")
    gender = Column(String(8), nullable=False, default="0", comment="性别, 0男1女")
    is_active = Column(Boolean, default=True, comment="是否可用")
    is_staff = Column(Boolean, default=False, comment="管理员")
    is_cancel = Column(Boolean, default=False, comment="是否注销")
    from_platform = Column(String(50), default="main", comment="来自平台/创建于哪个平台系统")
    last_ip = Column(String(50), nullable=True, comment="最后一次登录IP")
    last_login = Column(DateTime, nullable=True, comment="最近一次登录时间")
    wechat = Column(String(50), nullable=True, comment="微信")
    qq = Column(String(50), nullable=True, comment="QQ")

    def __repr__(self):
        return self.name

    # generate hash password
    @staticmethod
    def get_password_hash(password: str) -> str:
        return pwd_context.hash(password)

    # verify login password
    @staticmethod
    def verify_password(password: str, hashed_password: str) -> bool:
        return pwd_context.verify(password, hashed_password)


